//index.js
//获取应用实例
const app = getApp()
const Util = require('../../utils/util.js');
const Layer = require('../layer/layer.js');
import Poster from '../../miniprogram_dist/poster/poster';
Page({
  data: {
    hasImage:true,
    start: true,

    addPhoto:true,
    focus:false,

    inputname:'',
    mainData:{},

    animationPop: {},
    popLogin: {},

    token:''
  },
  onLoad: function (options) {
    var that = this;
   // wx.clearStorage();
    wx.showShareMenu({
      withShareTicket: true
    })
    that.getData();  
  },
  onShow: function (e) {
    var that = this;
    var aShow = app.aData.show;
    if (aShow) {
      that.getData();
    }
  },
  longTap: function (e) {
    console.log(e.type);
  },
  touchStart: function (e) {
    console.log(e.type);
  },
  touchEnd: function (e) {
    console.log(e.type);
  },
  getData:function(){
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.info(res);
        Util.showLoading();
        if (res.data) {
          Util.Main({
            "token": res.data
          }, function (re) {
            if(re.code==200){              
              that.setData({
                start: true,
                hasImage:false,                
                mainData:re.data,
                token: res.data
              })
            }else{
              that.setData({
                start: false,
                hasImage: true
              })
            }
          })
        }
        wx.hideLoading();
      },
      fail:function(){
        that.setData({
          start: false
        });
        Util.checkAuth(function (res) {
          if (res.authSetting['scope.userInfo']) {
            app.getUserInfo(true, function (userInfo) {
              if (typeof cb == "function") {
                cb(true);
              }
            });
          }
        });
      }
    })
  },
  setCateName: function (e) {
    var that = this;
    setTimeout(function () {      
      var content = that.data.inputname;
      if (!content) {
        Util.showMsgDialog("请输入相册名称");
        isdoing = false;
      }
      that.hidePhoto();
      wx.getStorage({
        key: 'token',
        success: function (res) {
          console.info(res);
          if (res.data) {
            Util.addCateName({
              "name": content,
              "token":res.data
            }, function (re) {
              if (re.code == 200) {
                wx.navigateTo({
                  url: '../main/content?cate_id=' + re.data
                })
              }
              else {
                Util.showMsgDialog(re.msg);
              }
            }, function (err) {
              Util.showMsgDialog("操作失败");
            })
          }
        }
      })
    }, 300);
  },
  photoDetail:function(e){
    var that = this;
    var cate_id = e.currentTarget.dataset.cateid;
    if (e.type =='longpress'){
      var token = that.data.token;
      Layer.layer.bottom('test', '取消', that, function () {
        Layer.layer.close(that);
      }, function () {
        Util.showLoading();
        Util.delPhoto({
          "cate_id": cate_id,
          "token": token
        }, function (re) {
          if (re.code == 200) {
            that.getData();
          }else {
            Util.showMsgDialog(re.msg);
          }
          wx.hideLoading();
        }, function (err) {
          Util.showMsgDialog("操作失败");
        })
      });
    }else{
      wx.navigateTo({
        url: '../main/content?cate_id='+cate_id
      });
    }
  },
  moreFunc:function(){
    wx.navigateTo({
      url: '../main/more'
    })
  },
  addPhoto:function(){
    var that = this;
    that.checkUser(-1, function (isauth) {
      console.info(isauth);
      if (isauth) {
        that.setData({
          addPhoto: false,
          focus: true
        })
      }else {
        mytoast.toast();
        Util.showMsgDialog("请先授权再使用");
      }
    })
  },
  bingData: function (e) {
    console.info(e.detail.value);
    this.setData({
      inputname: e.detail.value
    });
  },
  showPhoto: function (e) {
    console.info(e);
    this.setData({
      addPhoto: false,
      focus: true
    })
  },
  hidePhoto: function (e) {
    console.info(e);
    this.setData({
      addPhoto: true,
      focus: false
    })
  },
  scrolltoupper :function(){
    console.info(123);
    wx.startPullDownRefresh();
  },
  upper: function (e) {
    console.log(e)
  },
  lower: function (e) {
    console.log(e)
  },
  scroll: function (e) {
    console.log(e)
  },
  onPullDownRefresh:function(){
    wx.stopPullDownRefresh();
  },
  checkUser: function (action, cb) {
    var that = this;
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.info(res);
        if (res.data){
          cb(true);
        }
      }, 
      fail: function () {
        Util.checkAuth(function (res) {
          if (!res.authSetting['scope.userInfo']) {
            //未授权用户
            that.showLogin();
          }else{
            app.getUserInfo(true, function (userInfo) {
              if (typeof cb == "function") {
                cb(true);
              }
              that.getData();
            });
          } 
        });
      }
    })
  },
  showLogin: function () {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.scale(0).step()

    this.setData({
      popLogin: animation.export(),
      popLoginStatus: true
    })

    setTimeout(function () {
      animation.scale(1.0).step()
      this.setData({
        popLogin: animation
      })
    }.bind(this), 100)
  },
  showPop: function (status) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.scale(0).step()

    this.setData({
      animationPop: animation.export()
    })
    if (status == 1) {
      this.setData(
        {
          showPopStatus: true
        }
      );
    }
    setTimeout(function () {
      animation.scale(1.0).step()
      this.setData({
        animationPop: animation
      })
      if (status == 0) {
        this.setData(
          {
            showPopStatus: false,
          }
        );
      }
    }.bind(this), 300)
  },
  onGotUserInfo: function (e) {
    var that = this;
    if (e.detail.errMsg =='getUserInfo:ok'){
      Util.showLoading('授权中');
      app.getUserInfo(true, function (userInfo) {
        if (typeof cb == "function") {
          cb(true);
        }
        that.getData();
      });
    }
    that.setData({
      popLoginStatus: false
    })
  },
  setPopStatus: function (e) {
    this.showPop(e.currentTarget.dataset.status);
  },
  onShareAppMessage: function (res) {
    return {
      title: 'Hi,推荐你用用这个小程序',
      path: '/pages/index/index'
    }
  },
  onPosterSuccess(e) {
    const { detail } = e;
    wx.previewImage({
      current: detail,
      urls: [detail]
    })
  },
  onPosterFail(err) {
    console.info(123);
    console.error(err);
  },
})
